/**
 * 
 */
package com.teach.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.teach.Const;
import com.teach.entity.User;
import com.teach.service.UserLoginService;
import com.teach.service.impl.UserLoginServiceImpl;

/**
 * @author Liulianqiu
 *2020年8月6日
 */
@WebServlet("/userLogin.let")
public class UserLoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private UserLoginService userLogin = new UserLoginServiceImpl();
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置post请求正文的编码
		request.setCharacterEncoding("utf-8");
		//获取参数
		String userName = request.getParameter("userName");
		String userPassword = request.getParameter("userPassword");
		try {
			//调用业务逻辑接口，保存登录信息
			User user = userLogin.login(userName, userPassword);
			request.getSession().setAttribute(Const.LOGIN_USER_SESSION_NAME, user);
			//跳转页面
			response.sendRedirect(request.getContextPath()+"/admin/index.jsp");
			System.out.println(request.getContextPath());
		}catch (Exception e) {
			request.setAttribute("error",e.getMessage());
			request.getRequestDispatcher("/msg.jsp").forward(request, response);
			e.printStackTrace();
		}
	}
}
